package com.hxzy.controller.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hxzy.common.annotation.Log;
import com.hxzy.common.core.controller.BaseController;
import com.hxzy.common.core.domain.R;
import com.hxzy.common.core.domain.entity.SysDictType;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.enums.BusinessType;
import com.hxzy.common.utils.poi.ExcelUtil;
import com.hxzy.system.domain.dto.SysDictTypeSearchDTO;
import com.hxzy.system.domain.dto.ValueExistsDTO;
import com.hxzy.system.service.ISysDictTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


@Api(tags = "字典类型表")
@RestController
@RequestMapping(value = "/api/system/dict/type")
public class SysDictTypeController extends BaseController {

    @Autowired
    private ISysDictTypeService dictTypeService;

    @ApiOperation(value = "字典查询")
    @PreAuthorize("@hxzy.hasPermi('system:dict:list')")
    @GetMapping("/list")
    public R list(SysDictTypeSearchDTO  sysDictTypeSearchDTO)
    {
        IPage<SysDictType> list = this.dictTypeService.selectDictTypeList(sysDictTypeSearchDTO);
        return super.pageVO(list);
    }


    @ApiOperation(value = "导出字典类型")
    @PreAuthorize("@hxzy.hasPermi('system:dict:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDictTypeSearchDTO  sysDictTypeSearchDTO)
    {
        List<SysDictType> list = this.dictTypeService.searchExport(sysDictTypeSearchDTO);
        ExcelUtil<SysDictType> util = new ExcelUtil<SysDictType>(SysDictType.class);
        util.exportExcel(response, list, "字典类型");
    }


    @ApiOperation(value = "查询字典类型详细")
    @PreAuthorize("@hxzy.hasPermi('system:dict:query')")
    @GetMapping(value = "/{dictId}")
    public R getInfo(@PathVariable Long dictId)
    {
        SysDictType byId = this.dictTypeService.getById(dictId);
        byId.setUpdateBy(null);
        byId.setUpdateTime(null);
        return R.okHasData(byId);
    }


    @ApiOperation(value = "新增字典类型")
    @PreAuthorize("@hxzy.hasPermi('system:dict:add')")
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@Validated @RequestBody SysDictType dict)
    {
        ValueExistsDTO valueExistsDTO=new ValueExistsDTO(dict.getDictId(),dict.getDictName());
        int count = this.dictTypeService.checkDictTypeUnique(valueExistsDTO);

        if (count>0) {
            R r=R.build(AckCode.FAIL);
            r.setMsg("新增字典'" + dict.getDictType() + "'失败，字典类型已存在");
            return r;
        }

        return  this.toAjax( this.dictTypeService.save(dict));
    }


    @ApiOperation(value = "修改字典类型")
    @PreAuthorize("@hxzy.hasPermi('system:dict:edit')")
    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@Validated @RequestBody SysDictType dict)
    {
        ValueExistsDTO valueExistsDTO=new ValueExistsDTO(dict.getDictId(),dict.getDictName());
        int count = this.dictTypeService.checkDictTypeUnique(valueExistsDTO);

        if (count>0) {
            R r=R.build(AckCode.FAIL);
            r.setMsg("修改字典'" + dict.getDictType() + "'失败，字典类型已存在");
            return r;
        }

        return toAjax(dictTypeService.updateById(dict));
    }


    @ApiOperation(value = "删除字典类型")
    @PreAuthorize("@hxzy.hasPermi('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictIds}")
    public R remove(@PathVariable Long[] dictIds)
    {
        dictTypeService.deleteDictTypeByIds(dictIds);
        return R.ok();
    }


    @ApiOperation(value = "刷新字典缓存")
    @PreAuthorize("@hxzy.hasPermi('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessType.CLEAN)
    @DeleteMapping("/refreshCache")
    public R refreshCache()
    {
        dictTypeService.resetDictCache();
        return R.ok();
    }


    @ApiOperation(value = "获取字典选择框列表")
    @GetMapping("/optionselect")
    public R optionselect()
    {
        List<SysDictType> dictTypes = dictTypeService.selectDictTypeAll();
        return R.okHasData(dictTypes);
    }
}
